<?php

/*
 * 聊天记录单独使用数据表 chatlog
 */

namespace Home\Controller;

class MymessageController extends BaseMemberController {

    function _initialize() {
        parent::_initialize();
        $this->_user_id = session('visitor.user_id');
        $this->_message_mod = M('message');
        $this->_chatlog_mod = M('chatlog');
        $this->_member_mod = M('member');
    }

    /**
     * @api {get} /index.php?m=Home&c=Mymessage&a=status 获取聊天的未读条数系统消息未读条数
     * @apiName 获取聊天的未读条数系统消息未读条数  
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获取聊天的未读条数系统消息未读条数
     * 
     * @apiSuccess {String} systempm_count 系统未读消息条数
     * @apiSuccess {String} chatlog_count 聊天未读条数
     * 
     */
    function status() {
        //系统消息的未读条数
        $systempm_count = $this->_message_mod->where('from_id = ' . MSG_SYSTEM . ' AND to_id = ' . $this->_user_id . ' AND parent_id=0 AND new = 1 ')->count();
        //聊天消息的未读条数
        $chatlog_count = strval(count($this->_chatlog_mod->where('is_new = 1 AND receiverid=' . $this->_user_id)->group('senderid')->select()));

        render('10000', '', array('systempm_count' => $systempm_count, 'chatlog_count' => $chatlog_count));
    }

    /**
     * @api {get} /index.php?m=Home&c=Mymessage&a=systempm_list 获取系统消息的列表
     * 
     * @apiName 获取系统消息的列表  
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获取系统消息的列表
     * 
     * @apiSuccess {String} msg_id 系统未读消息条数
     * @apiSuccess {String} from_id 0表示为系统发出的消息
     * @apiSuccess {String} to_id 发送到的用户ID
     * @apiSuccess {String} content 消息内容
     * @apiSuccess {String} add_time 发送时间
     * @apiSuccess {String} new 是否为新消息
     */
    function systempm_list() {
        $condition = 'from_id = ' . MSG_SYSTEM . ' AND to_id = ' . $this->_user_id . ' AND parent_id=0 ';
        $message_list = $this->_message_mod->field('msg_id,from_id,to_id,content,add_time,new')->where($condition)->order('last_update DESC')->select();
        if($message_list){
            foreach ($message_list as $key => $message) {
                $message_list[$key]['add_time'] = date('Y-m-d H:i:s',$message['add_time']);
            }
        }
        //系统信息状态设置为已读
        $this->_message_mod->where($condition.' AND new = 1')->save(array('new'=>0));
        
        render('10000', '', array('message_list' => $message_list));
    }

    /**
     * @api {get} /index.php?m=Home&c=Mymessage&a=chat_list 获取聊天消息的列表(最近的聊天)
     * 
     * @apiName 获取聊天消息的列表  
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获取聊天消息的列表
     * 
     * @apiSuccess {String} contact_id 联系人ID
     * @apiSuccess {String} receiverid 接收人ID
     * @apiSuccess {String} senderid 发送人ID
     * @apiSuccess {String} sendtime 发送时间
     * @apiSuccess {String} sendcontent 发送内容
     * @apiSuccess {String} contact_name 联系人名称
     * @apiSuccess {String} contact_portrait 联系人头像
     */
    function chat_list() {
        //我最近的消息列表
        /*
        $sql = 'select * from ' . C('DB_PREFIX') . 'chatlog 
where senderid in(select senderid from ' . C('DB_PREFIX') . 'chatlog  where senderid = ' . $this->_user_id . ' or receiverid = ' . $this->_user_id . ' group by senderid) 
and sendtime in (select max(sendtime) from ' . C('DB_PREFIX') . 'chatlog  where senderid = ' . $this->_user_id . ' or receiverid = ' . $this->_user_id . ' group by senderid)order by sendtime';
        */
        $sql = 'SELECT * FROM '
                . '(SELECT receiverid as contact_id,receiverid,senderid,sendtime,sendcontent FROM ' . C('DB_PREFIX') . 'chatlog  WHERE (senderid = ' . $this->_user_id . ') AND (receiverid != ' . $this->_user_id . ') '
                . 'UNION SELECT senderid as contact_id,receiverid,senderid,sendtime,sendcontent FROM ' . C('DB_PREFIX') . 'chatlog WHERE (senderid != ' . $this->_user_id . ')  AND  (receiverid = ' . $this->_user_id . ')) '
                . 'as b GROUP BY contact_id ORDER BY max(sendtime) DESC;';
        $chat_list = $this->_chatlog_mod->query($sql);
        
        foreach ($chat_list as $key => $chat) {
            $contact = $this->_member_mod->field('user_name,portrait')->where('user_id='.$chat['contact_id'])->find();
            $chat_list[$key]['contact_name'] = $contact['user_name'];
            $chat_list[$key]['contact_portrait'] = $contact['portrait'];
            $chat_list[$key]['sendtime'] = date('Y-m-d H:i:s',$chat['sendtime']);
        }
        render('10000', '', array('chat_list' => $chat_list));
    }

    /**
     * @api {get} /index.php?m=Home&c=Mymessage&a=chatlog_list 获取聊天消息的聊天记录
     * 
     * @apiName 获取聊天消息的聊天记录  
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获取聊天消息的聊天记录
     * 
     * @apiParam {String} receiverid 接收消息者ID
     * 
     * @apiSuccess {String} senderid 发送人ID
     * @apiSuccess {String} receiverid 接收人ID
     * @apiSuccess {String} sendtime 发送时间
     * @apiSuccess {String} sendcontent 发送内容
     * @apiSuccess {String} sendername 发送人名称
     * @apiSuccess {String} senderportrait 发送人头像
     * @apiSuccess {String} receivername 接收人名称
     * @apiSuccess {String} receiverportrait 接收人头像

     * 
     */
    function chatlog_list() {
        //消息接收ID
        $receiverid = I('request.receiverid', 0, 'intval');
        //查看接收方信息
        $receiver_info = $this->_member_mod->field('user_id,user_name,portrait')->where(array('user_id' => $receiverid))->find();
        if (empty($receiver_info)) {
            render('19999', '用户不存在');
        }
        $where = '(receiverid=' . $receiverid . ' AND senderid = ' . $this->_user_id . ') OR (receiverid=' . $this->_user_id . ' AND senderid=' . $receiverid . ')';
        $chatlog_list = $this->_chatlog_mod->where($where)->order('sendtime asc')->select();
        
        $my_info = $this->_member_mod->field('user_id,user_name,portrait')->where(array('user_id' => $this->_user_id))->find();

        foreach ($chatlog_list as $key => $chatlog) {
            if ($chatlog['receiverid'] == $receiverid) {
                $chatlog_list[$key]['receivername'] = $receiver_info['user_name'];
                $chatlog_list[$key]['receiverportrait'] = $receiver_info['portrait'];
                $chatlog_list[$key]['sendername'] = $my_info['user_name'];
                $chatlog_list[$key]['senderportrait'] = $my_info['portrait'];
                $chatlog_list[$key]['is_my'] = 1;
            } else {
                $chatlog_list[$key]['receivername'] = $my_info['user_name'];
                $chatlog_list[$key]['receiverportrait'] = $my_info['portrait'];
                $chatlog_list[$key]['sendername'] = $receiver_info['user_name'];
                $chatlog_list[$key]['senderportrait'] = $receiver_info['portrait'];
                $chatlog_list[$key]['is_my'] = 0;
            }
            $chatlog_list[$key]['sendtime'] = date('Y-m-d H:i:s',$chatlog['sendtime']);
        }
        //把接收方为自己的消息全部设置为0,表示已读 
        $this->_chatlog_mod->where('is_new = 1 AND receiverid='.$this->_user_id)->save(array('is_new'=>0));
        render('10000', '', array('chatlog_list' => $chatlog_list));
    }

    /**
     * @api {get} /index.php?m=Home&c=Mymessage&a=send 发送消息
     * @apiName 发送消息  
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 发送消息
     * 
     * @apiParam {String} receiverid 接收消息者ID
     * @apiParam {String} sendcontent 消息内容
     * 
     */
    function send() {
        if (!IS_POST) {
            $receiverid = I('request.receiverid', 0, 'intval');
            if (empty($receiverid)) {
                render('19999', '用户名不能为空');
            }
            if ($receiverid == $this->_user_id) {
                render('19999', '不能给自己发送消息');
            }
            $sendcontent = I('request.sendcontent', '');
            if (empty($sendcontent)) {
                render('19999', '发送内容不能为空');
            }

            //查看当前用户名是否存在
            $member_mod = M('member');
            $member = $member_mod->where('user_id=' . $receiverid)->find();
            if (empty($member)) {
                render('19999', '用户不存在');
            }

            $data = array(
                'senderid' => $this->_user_id,
                'receiverid' => $receiverid,
                'sendtime' => time(),
                'sendcontent' => $sendcontent,
            );
            $result = $this->_chatlog_mod->add($data);
            if ($result) {
                render('10000', '发送消息成功');
            } else {
                render('19999', '发送消息失败');
            }
        }
    }

}